package com.cheyun.netsalev3.view.defeatdetails

import android.annotation.SuppressLint
import android.text.TextUtils
import android.util.Log
import android.webkit.WebView
import android.widget.LinearLayout
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.databinding.HistoricalRecordFragmentBinding
import com.cheyun.netsalev3.databinding.JchRecordVoiceFragmentBinding
import com.cheyun.netsalev3.tencenttim.utils.DemoLog
import com.cheyun.netsalev3.utils.BaseFragment
import com.cheyun.netsalev3.utils.api.GlobalConfig
import com.cheyun.netsalev3.viewmodel.ClueDetailsActivityViewModel
import com.just.agentweb.AgentWeb
import com.just.agentweb.WebChromeClient

class RecordVoiceFragment : BaseFragment<JchRecordVoiceFragmentBinding>() {
    override fun initContentView(): Int {
        return R.layout.jch_record_voice_fragment
    }

    override fun initViewModel(): ViewModel? {
        activity?.apply {
            return ViewModelProviders.of(this).get(ClueDetailsActivityViewModel::class.java)
        }
        return null
    }

    override fun initView() {

    }

    var mAgentWeb:AgentWeb? = null

    @SuppressLint("JavascriptInterface")
    override fun initData() {
        binding.viewModel?.recordVoice?.observe(this, Observer {
            var url = GlobalConfig.JCH_RECORDVOICE+it
            if(url.isNotEmpty()){
                mAgentWeb = AgentWeb.with(this)//传入Activity
                        .setAgentWebParent(binding.llWebview,  LinearLayout.LayoutParams(-1, -1))//传入AgentWeb 的父控件 ，如果父控件为 RelativeLayout ， 那么第二参数需要传入 RelativeLayout.LayoutParams
                        .useDefaultIndicator()
                        .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                        .createAgentWeb()
                        .ready()
                        .go( url)
            }

            DemoLog.i("DriveCalcActivity", url)

            mAgentWeb?.webCreator?.webView?.setWebChromeClient(object : WebChromeClient() {
                override fun onReceivedTitle(view: WebView, title: String) {
                    super.onReceivedTitle(view, title)
                }

                override fun onProgressChanged(view: WebView?, newProgress: Int) {
                    super.onProgressChanged(view, newProgress)
                    DemoLog.i("DriveBlockActivity","newProgress:"+newProgress)
                    mAgentWeb?.indicatorController?.setProgress(newProgress)
                }
            })

            mAgentWeb?.webCreator?.webView?.settings?.javaScriptEnabled = true
            mAgentWeb?.webCreator?.webView?.addJavascriptInterface(this, "callAndroid")
        })
    }

    override fun lazyInit() {
        Log.i(TAG,"override lazyinit")

    }

    companion object {
        fun newInstance() = RecordVoiceFragment()
    }


    override fun onResume() {
        mAgentWeb?.webLifeCycle?.onResume()
        super.onResume()
    }

    override fun onPause() {
        mAgentWeb?.webLifeCycle?.onPause()
        super.onPause()
    }

    override fun onDestroy() {
        mAgentWeb?.webLifeCycle?.onDestroy()
        super.onDestroy()
    }
}
